package com.yunxi.service.dubboService.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import com.github.pagehelper.PageInfo;
import com.yunxi.freeze.pojo.PartnerAmountFreezeFlowVO;
import com.yunxi.freeze.service.PartnerAmountFreezeFlowDubboService;
import com.yunxi.model.freeze.PartnerAccount;
import com.yunxi.model.freeze.PartnerAmountFreezeFlow;
import com.yunxi.service.localService.PartnerAccountInfoService;
import com.yunxi.service.localService.PartnerAmountFreezeFlowService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 渠道商冻结资金账户流水表 服务实现类
 * </p>
 *
 * @author chenmeng
 * @since 2019-07-23
 */

public class PartnerAmountFreezeFlowDubboServiceImpl implements PartnerAmountFreezeFlowDubboService {

    @Autowired
    @Qualifier(value = "partnerAmountFreezeFlowServiceImpl")
    PartnerAmountFreezeFlowService partnerAmountFreezeFlowService;

    @Autowired
    PartnerAccountInfoService partnerAccountInfoServiceImpl;

    @Override
    @Transactional
    public PartnerAmountFreezeFlowVO getById(String id) throws InvocationTargetException, IllegalAccessException {
        PartnerAmountFreezeFlow partnerAmountFreezeFlow = partnerAmountFreezeFlowService.getById(id);
        PartnerAmountFreezeFlowVO partnerAmountFreezeFlowVO = new PartnerAmountFreezeFlowVO();
        BeanUtils.copyProperties(partnerAmountFreezeFlow,partnerAmountFreezeFlowVO);
        return partnerAmountFreezeFlowVO;
    }

    @Override
    @Transactional
    public void saveOrUpdate(PartnerAmountFreezeFlowVO partnerAmountFreezeFlowVO) throws InvocationTargetException, IllegalAccessException {
        PartnerAmountFreezeFlow partnerAmountFreezeFlow = new PartnerAmountFreezeFlow();
        BeanUtils.copyProperties(partnerAmountFreezeFlowVO,partnerAmountFreezeFlow);
        partnerAmountFreezeFlowService.saveOrUpdate(partnerAmountFreezeFlow);
    }

    @Override
    @Transactional
    public void removeById(String id) {
    partnerAmountFreezeFlowService.removeById(id);
    }

    @Override
    @Transactional
    public PageInfo<PartnerAmountFreezeFlowVO> findPageByVO(PartnerAmountFreezeFlowVO partnerAmountFreezeFlowVO, Integer pageNum, Integer pageSize) throws InvocationTargetException, IllegalAccessException {
      PageInfo<PartnerAmountFreezeFlowVO> pageInfo = partnerAmountFreezeFlowService.pageByVo(partnerAmountFreezeFlowVO,pageNum,pageSize);
      return pageInfo;
   }

    @Override
    @Transactional
    public void removeByListVo(List<PartnerAmountFreezeFlowVO> partnerAmountFreezeFlowVOList) throws IllegalAccessException, InvocationTargetException, InstantiationException {
        partnerAmountFreezeFlowService.removeByListVo(partnerAmountFreezeFlowVOList);
    }

    @Override
    @Transactional
    public List listByVo(PartnerAmountFreezeFlowVO partnerAmountFreezeFlowVO) {
      return partnerAmountFreezeFlowService.listByVo(partnerAmountFreezeFlowVO);
    }

    @Override
    @Transactional
    public boolean unfreezeAmountByOrderCarId(String orderCarId){
        QueryWrapper qw = new QueryWrapper();
        qw.eq("ORDER_CAR_ID",orderCarId);
        qw.eq("FREEZE_STATUS",1); //1表示已冻结
        PartnerAmountFreezeFlow flow = partnerAmountFreezeFlowService.getOne(qw);

        if(flow != null){
            QueryWrapper qwPartnerAccount = new QueryWrapper();
            qwPartnerAccount.eq("PARTNER_CODE",flow.getPartnerCode());
            qwPartnerAccount.eq("ACCOUNT_CODE",flow.getAccountCode());
            PartnerAccount partnerAccount = partnerAccountInfoServiceImpl.getOne(qwPartnerAccount);
            //加上已冻结的资金
            partnerAccount.setFrozenAmount(partnerAccount.getFrozenAmount().subtract(flow.getFreezeAmount()));
            partnerAccountInfoServiceImpl.updateById(partnerAccount);

            //
            //插入一条明细解冻。状态为2
            PartnerAmountFreezeFlow jieDongFlow = new PartnerAmountFreezeFlow();
            jieDongFlow.setAccountCode(flow.getAccountCode());
            jieDongFlow.setFreezeStatus(2); //解冻
            jieDongFlow.setOrderCarId(orderCarId);
            jieDongFlow.setPartnerCode(flow.getPartnerCode());
            jieDongFlow.setVin(flow.getVin());
            jieDongFlow.setCreateDate(new Date());
            BigDecimal unFreezeAmount = new BigDecimal(0).subtract(flow.getFreezeAmount());
            jieDongFlow.setFreezeAmount(unFreezeAmount);
            partnerAmountFreezeFlowService.save(jieDongFlow);
        }
        return true;
    }

    /**
     * 解冻资金
     * @return
     */
    @Override
    @Transactional
    public boolean unfreezeAmount(String vin){
        QueryWrapper qw = new QueryWrapper();
        qw.eq("VIN",vin);
        qw.eq("FREEZE_STATUS",1); //1表示已冻结
        PartnerAmountFreezeFlow flow = partnerAmountFreezeFlowService.getOne(qw);

        if(flow != null){
            QueryWrapper qwPartnerAccount = new QueryWrapper();
            qwPartnerAccount.eq("PARTNER_CODE",flow.getPartnerCode());
            qwPartnerAccount.eq("ACCOUNT_CODE",flow.getAccountCode());
            PartnerAccount partnerAccount = partnerAccountInfoServiceImpl.getOne(qwPartnerAccount);
            //加上已冻结的资金
            partnerAccount.setFrozenAmount(partnerAccount.getFrozenAmount().add(flow.getFreezeAmount()));
            partnerAccountInfoServiceImpl.updateById(partnerAccount);
        }
        return true;
    }
}
